package com.example.nutritionmanager

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Send
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.rememberNavController
import com.example.nutritionmanager.conversationApi.SSEClient
import com.example.nutritionmanager.viewModels.AIChatViewModel

@Composable
fun AIScreen(navController: NavController, viewModel: AIChatViewModel = hiltViewModel()) {
    val colorScheme = MaterialTheme.colorScheme
    val messages by viewModel.messages.collectAsState()
    val listState = rememberLazyListState()

    var userMessage by remember { mutableStateOf("") }
//    var messages by remember { mutableStateOf(listOf<Message>()) }
//    var isEditing by remember { mutableStateOf(false) } // 标记是否开始编辑输入框
    val defaultText = "用户性别男，年龄20岁，身高180cm，体重80kg，减脂期。今日已摄入：碳水100g，蛋白20g，脂肪20g，k：30mg。当前摄入食物：碳水10g，脂肪30g，蛋白质0g" // 默认内容

    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState()) // 使页面可滑动
            .background(colorScheme.background)  // 设置背景为灰色
            .padding(16.dp)
            .padding(bottom = 60.dp)
    ) {
        Text(
            text = "AI助手",
            fontSize = 22.sp,
            color = colorScheme.onBackground,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        // 显示聊天记录
        LazyColumn(modifier = Modifier.weight(1f)) {
            items(messages) { message ->
                MessageBubble(message = message)
            }
        }

        Spacer(modifier = Modifier.height(8.dp))

        // 输入框
        Row(
            modifier = Modifier
                .height(64.dp)
                .fillMaxWidth()
                .border(width = 1.dp, color = Color.Gray)
                .padding(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            BasicTextField(
                value = userMessage,
                onValueChange = {
                    userMessage = it
                },
                textStyle = TextStyle(fontSize = MaterialTheme.typography.bodyLarge.fontSize),
                modifier = Modifier
                    .weight(1f)
                    .background(colorScheme.surfaceVariant, MaterialTheme.shapes.small)
                    .padding(12.dp)
                    .fillMaxHeight()
                    .heightIn(min = 56.dp, max = 200.dp) // 动态调整高度
            )
//            if (userMessage.isEmpty()) {
//                Text(
////                    text = "用户性别男，年龄20岁，身高180cm，体重80kg，减脂期。今日已摄入：碳水100g，蛋白20g，脂肪20g，k：30mg。当前摄入食物：碳水10g，脂肪30g，蛋白质0g",
//                    style = TextStyle(color = Color.Gray, fontSize = 16.sp),
//                    modifier = Modifier.padding(12.dp)
//                )
//            }
            // 显示默认内容，当输入框为空时
//            if (userMessage.isEmpty() && !isEditing) {
//                Text(
//                    text = defaultText, // 默认内容
//                    style = TextStyle(color = Color.Gray, fontSize = 16.sp),
//                    modifier = Modifier.padding(12.dp)
//                )
//            }
            Spacer(modifier = Modifier.width(8.dp))
            IconButton(onClick = {
//                // 发送用户消息或默认内容
//                val messageToSend = if (userMessage.isEmpty()) defaultText else userMessage
//                messages = messages + Message(text = messageToSend, isUser = true)
//                var textResponse = ""
//                messages = messages + Message(text = textResponse, isUser = false)

                // 发起 SSE 请求
//                sseClient.startSSERequest(messageToSend, object : SSEClient.MessageListener {
//                    override fun onAppendMessage(text: String) {
//                        // 动态更新最后一条消息
//                        messages = messages.toMutableList().apply {
//                            this[lastIndex] = this[lastIndex].copy(text = this[lastIndex].text + text)
//                        }
//                    }
//
//                    override fun onFinish() {
//                        // 完成时可以进行其他操作
//                    }
//                })
                viewModel.sendMessage(userMessage)
                userMessage = "" // 发送后清空输入框
            }) {
                Icon(Icons.Filled.Send, contentDescription = "Send")
            }
        }
    }
}



